package com.example.helloworld;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.helloworld.view.Frag01;
import com.example.helloworld.view.Frag02;
import com.example.helloworld.view.Frag03;

@SuppressLint("NewApi")
public class OptionMenu extends Activity {

	private ActionBar actionbar;
	private EditText searchEdit;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_option_menu);
		
		actionbar =	getActionBar(); 
		actionbar.setSubtitle("inspect option menu & action bar");
		actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		
		Tab tab01 =	actionbar.newTab();
		tab01.setText("Product #1");
		tab01.setTabListener(new ProductTabListener(this, Frag01.class.getName()));
		actionbar.addTab(tab01);

		Tab tab02 =	actionbar.newTab();
		tab02.setText("Product #2");
		tab02.setTabListener(new ProductTabListener(this, Frag02.class.getName()));
		actionbar.addTab(tab02);

		Tab tab03 =	actionbar.newTab();
		tab03.setText("Product #2");
		tab03.setTabListener(new ProductTabListener(this, Frag03.class.getName()));
		actionbar.addTab(tab03);
	}
	
	private class ProductTabListener implements ActionBar.TabListener {
		private Fragment fragment;
		private final Activity activity;
		private final String name;
		
		public ProductTabListener(Activity activity, String name) {
			this.activity =	activity;
			this.name =		name;
		}
		
		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) { }

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			fragment =	Fragment.instantiate(activity, name);
			ft.add(android.R.id.content, fragment);
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			ft.remove(fragment);
			fragment =	null;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.option_menu, menu);
		
		View view =		menu.findItem(R.id.menu_search).getActionView();
		searchEdit =	(EditText)view.findViewById(R.id.searchEdit);
		
		if (searchEdit != null) {
			searchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
				@Override
				public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
					if (arg2 == null || arg2.getAction() == KeyEvent.ACTION_UP) {
						search();
						
						((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(arg0.getWindowToken(), 0);
					}
					return false;
				}
			});
		}
		return true;
	}
	
	private void search() {
		final String searchString =	searchEdit.getEditableText().toString();
		Toast.makeText(this, "Search word : " + searchString, Toast.LENGTH_SHORT).show();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id =	item.getItemId();
		
		switch (id) {
		case android.R.id.home:
			Toast.makeText(this, "Home menu selected.", Toast.LENGTH_SHORT).show();
			break;
		case R.id.menu_refresh:
			Toast.makeText(this, "Refresh menu selected.", Toast.LENGTH_SHORT).show();
			break;
		case R.id.menu_search:
			Toast.makeText(this, "Search menu selected.", Toast.LENGTH_SHORT).show();
			break;
		case R.id.menu_settings:
			Toast.makeText(this, "Setting menu selected.", Toast.LENGTH_SHORT).show();
			break;
		default:
			Toast.makeText(this, "Some menu selected. (id:" + id + ")", Toast.LENGTH_SHORT).show();
			break;
		}
		
		return super.onOptionsItemSelected(item);
	}

	public void callChangeActionBar(View view) {
		actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_USE_LOGO | ActionBar.DISPLAY_HOME_AS_UP);
	}
	
	public void showMessage(View view) {
		Toast.makeText(this, "fragment01 message", Toast.LENGTH_SHORT).show();
	}
	
}
